﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kristall.Screens;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Kristall.Engine;

namespace Kristall.Editor.Commands
{
    public class SplitCommand : MacroCommand
    {
        string _type;
        Vector2 _splitPosition;
        Animatable _toSplit;


        public SplitCommand(Level level, Animatable toSplit, Vector2 splitPosition, string type)
        {
            Animatable animatable = toSplit;

            if (animatable != null)
            {
                _type = type;
                _toSplit = animatable;
                _splitPosition = splitPosition;

                DuplicateCommand duplicateA = new DuplicateCommand(level, animatable, animatable.Position);
                DuplicateCommand duplicateB = new DuplicateCommand(level, animatable, animatable.Position);

                this.Commands.Add(duplicateA);

                if (type == "Vertical")
                {
                    int aWidth = (int)splitPosition.X - toSplit.Bounds.Left;
                    int bWidth = toSplit.Bounds.Right - (int)splitPosition.X;
                    this.Commands.Add(new ResizeCommand(duplicateA.Duplicate, aWidth, toSplit.Bounds.Height));

                    this.Commands.Add(duplicateB);
                    this.Commands.Add(new ResizeCommand(duplicateB.Duplicate, bWidth, toSplit.Bounds.Height));
                    this.Commands.Add(new MoveCommand(duplicateB.Duplicate, new Vector2(splitPosition.X, toSplit.Bounds.Top)));
                }
                else
                {
                    int aHeight = (int)splitPosition.Y - toSplit.Bounds.Top;
                    int bHeight = toSplit.Bounds.Bottom - (int)splitPosition.Y;
                    this.Commands.Add(new ResizeCommand(duplicateA.Duplicate, toSplit.Bounds.Width, bHeight));

                    this.Commands.Add(duplicateB);
                    this.Commands.Add(new ResizeCommand(duplicateB.Duplicate, toSplit.Bounds.Width, bHeight));
                    this.Commands.Add(new MoveCommand(duplicateB.Duplicate, new Vector2(toSplit.Bounds.Left, splitPosition.Y)));
                }

                this.Commands.Add(new ResizeAnimationCommand((Animatable)duplicateA.Duplicate));
                this.Commands.Add(new ResizeAnimationCommand((Animatable)duplicateB.Duplicate));

                this.Commands.Add(new DeleteCommand(level, _toSplit));

            }
        }
    }
}
